﻿using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.DataUpdater.Validation;
using ExpenseSystem.Fether;
using ExpenseSystem.Model;
using System;

namespace ExpenseSystem.Core.OrderModules
{
    public class OrderDetailUpdaterRegister
    {
        int rows;
        public NormalDataUpdater<OrderDetailModel> GetOrderDetailUpdater()
        {
            NormalDataUpdater<OrderDetailModel> systemDataUpdater = new NormalDataUpdater<OrderDetailModel>();
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += PriceValid;
            systemDataUpdater.DataUpdating += DocExist;
            systemDataUpdater.DataUpdating += StateValid;

            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        private void CheckAvaliable(object sender, DataValidationEventArgs<OrderDetailModel> e)
        {
            if (e.Delete.State != Definition.STATE_NORMAL)
            {
                e.Cancle = true;
                e.Message = String.Format("[{0}]正在生效，不能删除", e.Delete.AutoId);
            }
        }

        private void DataNotSpaceValid(object sender, DataValidationEventArgs<OrderDetailModel> e)
        {
            if(String.IsNullOrWhiteSpace(e.Insert.ProductionName))
            {
                e.Cancle = true;
                e.Message = "产品名称不能为空";
                return;
            }

        }
        private void DocExist(object sender, DataValidationEventArgs<OrderDetailModel> e)
        {
            string message;
            OrderFetcher orderFetcher = new OrderFetcher();
            ProductionFetcher productionFetcher = new ProductionFetcher();
            OrderModel orderModel = orderFetcher.Get(e.Insert.Site, e.Insert.OrderId, out message);
            if (orderModel == null )
            {
                e.Cancle = true;
                e.Message = "错误的来源订单号";
                return;
            }
            ProductionModel productionModel = productionFetcher.Get(e.Insert.Site, e.Insert.ProductionId, out message);
            if(productionModel==null||e.Insert.UnitGroupId!=productionModel.UnitGroupId||
                e.Insert.UnitId!=productionModel.UnitId||e.Insert.SalesUnitId!=productionModel.SalesUnitId)
            {
                e.Cancle = true;
                e.Message = "错误的产品信息";
                return;
            }

        }
        private void PriceValid(object sender, DataValidationEventArgs<OrderDetailModel> e)
        {
            string message;
            QuoteFetcher quoteFetcher = new QuoteFetcher();
            QuoteModel quoteModel = quoteFetcher.Get(e.Insert.Site, e.Insert.QuoteId, out message);
            if (quoteModel == null || quoteModel.State == Definition.STATE_READY || quoteModel.State == Definition.STATE_NORMAL)
            {
                e.Cancle = true;
                e.Message = "无法找到有效的报价单";
                return;
            }
            DateTime  dt2, dt3;
            dt2 = dt3 = DateTime.MinValue;
            DateTime.TryParse(quoteModel.OpenDate, out dt2);
            DateTime.TryParse(quoteModel.DueDate, out dt3);
            if(dt2>DateTime.Now||dt3<=DateTime.Now)
            {
                e.Cancle = true;
                e.Message = String.Format("报价单{0}的生效期间{1}-{2}不符合", quoteModel.Code, dt2, dt3);
                return;
            }
            if (e.Insert.Price<e.Insert.PriceRef3&&!e.Insert.Free)
            {
                e.Cancle = true;
                e.Message = String.Format("当前价格{0}低于最低限价{1}", e.Insert.Price, e.Insert.PriceRef3);
                return;
            }
            if(Math.Abs(e.Insert.Price*e.Insert.Quantity1-e.Insert.Amount)>((decimal)0.01))
            {
                e.Cancle = true;
                e.Message = String.Format("小计{0}不等于单价{0}乘以数量{2}", e.Insert.Amount, e.Insert.Price, e.Insert.Quantity1);
                return;
            }
            if (e.Insert.Free && e.Insert.Price != 0)
            {
                e.Cancle = true;
                e.Message = String.Format("免费品不能有价格", e.Insert.Price, e.Insert.PriceRef3);
                return;
            }
        }
        private void StateValid(object sender, DataValidationEventArgs<OrderDetailModel> e)
        {
            string message;
            OrderFetcher serviceOrderFetcher = new OrderFetcher();
            OrderModel model = serviceOrderFetcher.Get(e.Insert.Site, e.Insert.OrderId, out message);
            if (e.Insert.AutoId == 0 && model != null && model.State != Definition.STATE_NORMAL)
            {
                e.Message = String.Format("已审核的表单{0}不能新增明细行",model.OrderCode);
                e.Cancle = true;
                return;
            }
            if (e.Insert.State != Definition.STATE_NORMAL)
            {
                if (e.Insert.PropertyUpdated.Contains("ProductionId") ||
                    e.Insert.PropertyUpdated.Contains("OrderId") ||
                    e.Insert.PropertyUpdated.Contains("ProductionName") ||
                    e.Insert.PropertyUpdated.Contains("SalesUnitId") ||
                    e.Insert.PropertyUpdated.Contains("DeliveryDate") ||

                    e.Insert.PropertyUpdated.Contains("Price") ||
                    e.Insert.PropertyUpdated.Contains("Amount") ||
                    e.Insert.PropertyUpdated.Contains("QuoteId") ||
                    e.Insert.PropertyUpdated.Contains("Quantity1"))
                {
                    e.Message = "已审核的表单不能修改";
                    e.Cancle = true;
                    return;
                }
            }
            if (e.Insert.State == Definition.STATE_NORMAL)
            {
                if (e.Insert.AutoId > 0)
                {
                    PayFetcher payBonusFetcher = new PayFetcher();
                    DesignerBonusFetcher designerBonusFetcher = new DesignerBonusFetcher();
                    SalesBonusFetcher salesBonusFetcher = new SalesBonusFetcher();
                    ServiceBonusFetcher serviceBonusFetcher = new ServiceBonusFetcher();
                    DeliveryDetailFetcher deliveryFetcher = new DeliveryDetailFetcher();
                    if (payBonusFetcher.ListByOrder(e.Insert.Site, e.Insert.OrderId, out message, out rows).Count > 0)
                    {
                        e.Message = "已生成下游付款单据的不允许进行反审核";
                        e.Cancle = true;
                        return;
                    }
                    if (designerBonusFetcher.ListByOrder(e.Insert.Site, e.Insert.OrderId, out message, out rows).Count > 0)
                    {
                        e.Message = "已生成下游设计费用单据的不允许进行反审核";
                        e.Cancle = true;
                        return;
                    }
                    if (salesBonusFetcher.ListByOrder(e.Insert.Site, e.Insert.OrderId, out message, out rows).Count > 0)
                    {
                        e.Message = "已生成下游销售费用单据的不允许进行反审核";
                        e.Cancle = true;
                        return;
                    }
                    if (serviceBonusFetcher.ListByOrder(e.Insert.Site, e.Insert.OrderId, out message, out rows).Count > 0)
                    {
                        e.Message = "已生成下游服务费用单据的不允许进行反审核";
                        e.Cancle = true;
                        return;
                    }
                    if (deliveryFetcher.ListByOrder(e.Insert.Site, e.Insert.OrderId, out message, out rows).Count > 0)
                    {
                        e.Message = "已生成下游物流单据的不允许进行反审核";
                        e.Cancle = true;
                        return;
                    }
                }
            }
        }
    }
}
